﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using Burraco.Library;

namespace Burraco.WinApp
{
    public partial class FormGriglia : Form
    {
        public Torneo Torneo { get; set; }

        private void setModalitaSessoFromCombo(ModalitaSesso ms)
        {
            switch (ms)
            {
                case ModalitaSesso.TuttiControTutti: cmbModalitaSesso.SelectedIndex = 0; break;
                case ModalitaSesso.MaschiVsFemmine: cmbModalitaSesso.SelectedIndex = 1; break;
                case ModalitaSesso.MaschioConFemmina: cmbModalitaSesso.SelectedIndex = 2; break;
                case ModalitaSesso.SessoUnico: cmbModalitaSesso.SelectedIndex = 3; break;
            }
        }

        private void setModalitaCompagniFromCombo(ModalitaCompagni mc)
        {
            switch (mc)
            {
                case ModalitaCompagni.Mai: cmbModalitaCompagni.SelectedIndex = 0; break;
                case ModalitaCompagni.SiAvversarioNoCoppia: cmbModalitaCompagni.SelectedIndex = 1; break;
                case ModalitaCompagni.SiCoppiaNoAvversario: cmbModalitaCompagni.SelectedIndex = 2; break;
                case ModalitaCompagni.Sempre: cmbModalitaCompagni.SelectedIndex = 3; break;
                case ModalitaCompagni.CoppieFisse: cmbModalitaCompagni.SelectedIndex = 4; break;
            }
        }

        public void FillGriglia()
        {
            Torneo t = this.Torneo;

            numPartecipanti.Value = t.getListaGiocatori().Count;
            setModalitaSessoFromCombo(t.RegolaSesso);
            setModalitaCompagniFromCombo(t.RegolaCompagni);
            numLimiteCoppia.Value = t.LimiteRipetiCoppia;
            numLimiteAvversari.Value = t.LimiteRipetiAvversario;
            numLimiteTavolo.Value = t.LimiteRipetiTavolo;
            numLimiteTavoloZoppo.Value = t.LimiteRipetiInTavoloZoppo;
            numLimiteTavoloZoppoDa2.Value = t.LimiteRipetiInTavoloZoppoDa2;
            numLimiteTavoloZoppoDa3.Value = t.LimiteRipetiInTavoloZoppoDa3;
            numPartite.Value = t.NumeroPartite;




            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
            dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.50F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
            dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;

            List<Partita> partite = t.getListaPartite();
            TemplateCella tc = new TemplateCella();
            int i = 0;
            foreach (Partita p in partite)
            {
                i++;
                DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
                col1.Width = 30;
                col1.HeaderText = "Partita " + i;
                col1.Name = "p" + i + "-c1";
                col1.DefaultCellStyle = dataGridViewCellStyle1;
                grdTorneo.Columns.Add(col1);

                DataGridViewTextBoxColumn colmezzo1 = new DataGridViewTextBoxColumn();
                colmezzo1.Width = 10;
                grdTorneo.Columns.Add(colmezzo1);
                DataGridViewTextBoxColumn colmezzo2 = new DataGridViewTextBoxColumn();
                colmezzo2.Width = 10;
                grdTorneo.Columns.Add(colmezzo2);

                DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
                col2.Width = 30;
                col2.HeaderText = "Partita " + i;
                col2.Name = "p" + i + "-c2";
                col2.DefaultCellStyle = dataGridViewCellStyle1;
                grdTorneo.Columns.Add(col2);

                DataGridViewTextBoxColumn colmezzo3 = new DataGridViewTextBoxColumn();
                colmezzo3.Width = 10;
                grdTorneo.Columns.Add(colmezzo3);
                DataGridViewTextBoxColumn colmezzo4 = new DataGridViewTextBoxColumn();
                colmezzo4.Width = 10;
                grdTorneo.Columns.Add(colmezzo4);


            }


            Partita partita = t.getListaPartite().ElementAt(0);

            if (partita.TavoloDa2 != null)
            {
                grdTorneo.Rows.Add();
                grdTorneo.Rows.Add();
            }
            if (partita.TavoloDa3 != null)
            {
                grdTorneo.Rows.Add();
                grdTorneo.Rows.Add();
            }

            foreach (TavoloDa4 tav in partita.getListaTavoliDa4())
            {
                grdTorneo.Rows.Add();
                grdTorneo.Rows.Add();
            }

            i = 0;
            foreach (Partita p in partite)
            {
                i++;
                int j = -1;
                if (p.TavoloDa2 != null)
                {
                    DataGridViewRow riga;

                    j++;
                    riga = grdTorneo.Rows[j];
                    riga.Cells["p" + i + "-c1"].Value = p.TavoloDa2.getGiocatore(1).NomeCompleto.Trim();

                    j++;
                    riga = grdTorneo.Rows[j];
                    riga.Cells["p" + i + "-c1"].Value = p.TavoloDa2.getGiocatore(2).NomeCompleto.Trim();
                }
                if (p.TavoloDa3 != null)
                {
                    DataGridViewRow riga;
                    j++;
                    riga = grdTorneo.Rows[j];
                    riga.Cells["p" + i + "-c1"].Value = p.TavoloDa3.getGiocatore(1).NomeCompleto.Trim();
                    riga = grdTorneo.Rows[j];
                    riga.Cells["p" + i + "-c2"].Value = p.TavoloDa3.getGiocatore(2).NomeCompleto.Trim();
                    j++;
                    riga = grdTorneo.Rows[j];
                    riga.Cells["p" + i + "-c1"].Value = p.TavoloDa3.getGiocatore(3).NomeCompleto.Trim();
                }

                foreach (TavoloDa4 tav in p.getListaTavoliDa4())
                {
                    DataGridViewRow riga;
                    j++;
                    riga = grdTorneo.Rows[j];
                    riga.Cells["p" + i + "-c1"].Value = tav.getCoppia(1).getGiocatore(1).NomeCompleto.Trim();
                    riga = grdTorneo.Rows[j];
                    riga.Cells["p" + i + "-c2"].Value = tav.getCoppia(1).getGiocatore(2).NomeCompleto.Trim();

                    j++;
                    riga = grdTorneo.Rows[j];
                    riga.Cells["p" + i + "-c1"].Value = tav.getCoppia(2).getGiocatore(1).NomeCompleto.Trim();
                    riga = grdTorneo.Rows[j];
                    riga.Cells["p" + i + "-c2"].Value = tav.getCoppia(2).getGiocatore(2).NomeCompleto.Trim();

                }
            }
            if (grdTorneo.Rows.Count < 10) 
            {
                for (int r = grdTorneo.Rows.Count; grdTorneo.Rows.Count < 5; r++)
                {
                    grdTorneo.Rows.Add();
                }
            }

            DataGridViewCellStyle stileCentrato = new DataGridViewCellStyle();
            DataGridViewCellStyle allDestra = new DataGridViewCellStyle();
            stileCentrato.Alignment = DataGridViewContentAlignment.MiddleCenter;
            allDestra.Alignment = DataGridViewContentAlignment.MiddleRight;

            grdTorneo.Columns.Add(new DataGridViewTextBoxColumn());

            grdTorneo.Columns[grdTorneo.Columns.Count - 2].Width = 70;
            grdTorneo.Columns[grdTorneo.Columns.Count - 1].Width = 90;
            for (int n = 0; n < 5; n++)
            {
                grdTorneo.Rows[(n)].Cells[grdTorneo.Columns.Count - 2].Style = allDestra;
            }
            grdTorneo.Rows[(0)].Cells[grdTorneo.Columns.Count - 2].Value = " N° partec";
            grdTorneo.Rows[(1)].Cells[grdTorneo.Columns.Count - 2].Value = " Mod Sesso";
            grdTorneo.Rows[(2)].Cells[grdTorneo.Columns.Count - 2].Value = "  Compagni";
            grdTorneo.Rows[(3)].Cells[grdTorneo.Columns.Count - 2].Value = "N° partite";
            grdTorneo.Rows[(4)].Cells[grdTorneo.Columns.Count - 2].Value = "Rip coppia";
            grdTorneo.Rows[(0)].Cells[grdTorneo.Columns.Count - 1].Value = t.getListaGiocatori().Count;
            grdTorneo.Rows[(1)].Cells[grdTorneo.Columns.Count - 1].Value = t.RegolaSesso.ToString();
            grdTorneo.Rows[(2)].Cells[grdTorneo.Columns.Count - 1].Value = t.RegolaCompagni.ToString();
            grdTorneo.Rows[(3)].Cells[grdTorneo.Columns.Count - 1].Value = t.NumeroPartite;
            grdTorneo.Rows[(4)].Cells[grdTorneo.Columns.Count - 1].Value = t.LimiteRipetiCoppia;

            grdTorneo.Columns.Add(new DataGridViewTextBoxColumn());
            grdTorneo.Columns.Add(new DataGridViewTextBoxColumn());


            grdTorneo.Columns[grdTorneo.Columns.Count - 2].Width = 75;
            grdTorneo.Columns[grdTorneo.Columns.Count - 1].Width = 25;

            for (int n = 0; n < 5; n++)
            {
                grdTorneo.Rows[(n)].Cells[grdTorneo.Columns.Count - 2].Style = allDestra;
            }

            grdTorneo.Rows[(0)].Cells[grdTorneo.Columns.Count - 2].Value = " Ripet avvers";
            grdTorneo.Rows[(1)].Cells[grdTorneo.Columns.Count - 2].Value = " Ripet tavolo";
            grdTorneo.Rows[(2)].Cells[grdTorneo.Columns.Count - 2].Value = "Rip tav zoppo";
            grdTorneo.Rows[(3)].Cells[grdTorneo.Columns.Count - 2].Value = "Rip tav zop 2";
            grdTorneo.Rows[(4)].Cells[grdTorneo.Columns.Count - 2].Value = "Rip tav zop 3";

            for (int n = 0; n < 5; n++)
            {
                grdTorneo.Rows[(n)].Cells[grdTorneo.Columns.Count - 1].Style = stileCentrato;
            }
            grdTorneo.Rows[(0)].Cells[grdTorneo.Columns.Count - 1].Value = t.LimiteRipetiAvversario;
            grdTorneo.Rows[(1)].Cells[grdTorneo.Columns.Count - 1].Value = t.LimiteRipetiTavolo;
            grdTorneo.Rows[(2)].Cells[grdTorneo.Columns.Count - 1].Value = t.LimiteRipetiInTavoloZoppo;
            grdTorneo.Rows[(3)].Cells[grdTorneo.Columns.Count - 1].Value = t.LimiteRipetiInTavoloZoppoDa2;
            grdTorneo.Rows[(4)].Cells[grdTorneo.Columns.Count - 1].Value = t.LimiteRipetiInTavoloZoppoDa3;


        }

        public FormGriglia()
        {
            InitializeComponent();



        }

    }

    public class TemplateCella : DataGridViewCell
    {
        public TemplateCella()
        {
        }

    }
}
